package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// DeviceLog 设备操作日志
type DeviceLog struct {
	ID           string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	RobotID      string `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:idx_robotID_createdDate,priority:1;comment:机器人id"`
	LogCategory  int    `json:"log_category" gorm:"column:log_category;type:int;size:16;comment:事件分类"`
	LogType      int    `json:"log_type" gorm:"column:log_type;type:int;size:16;comment:事件类型"`
	Content      string `json:"content" gorm:"column:content;type:varchar(1024);comment:事件内容"`
	CreatedMonth string `json:"created_month,omitempty" gorm:"column:created_month;type:string;size:10;index:created_month;comment:上报月份"`
	CreatedDate  string `json:"created_date,omitempty" gorm:"column:created_date;type:string;size:10;index:idx_robotID_createdDate,priority:2;comment:上报日期"`
	CreatedAt    int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
}

// TableName 指定表名
func (DeviceLog) TableName() string {
	return "device_log"
}

// BeforeCreate 自增id
func (d *DeviceLog) BeforeCreate(tx *gorm.DB) (err error) {

	if d.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	d.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &DeviceLog{})
}

type DeviceLogTpl struct {
	DeviceLog
}

func (DeviceLogTpl) TableName() string {
	return "device_log_tpl"
}
func (DeviceLogTpl) Unimplemented() {

}
